package mapreduce.sortable;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class SortMapper extends Mapper<LongWritable, Text, MyComprable, NullWritable> {
    /*Map将K1,V1转成K2,V2
    K1       V1
    0        a  1
    4        b  3
    ---------------------
    K2       V2
    (a,1)    null
    (b,3)    null
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1.拆分文件行
        String[] splitLine = value.toString().split("\t");
        //2.创建对象
        MyComprable myComprable = new MyComprable(splitLine[0], Integer.parseInt(splitLine[1]));

        //2:将K2和V2写入上下文中
        context.write(myComprable, NullWritable.get());
    }
}
